package com.adfdi.model;

import com.adfdi.model.common.AppModule;
import com.adfdi.model.view.DepartmentsVOImpl;
import com.adfdi.model.view.DepartmentsVORowImpl;
import com.adfdi.model.view.DeptTransVOImpl;
import com.adfdi.model.view.DeptTransVORowImpl;
import com.adfdi.model.view.EmplDIVOImpl;
import com.adfdi.model.view.EmplDIVORowImpl;
import com.adfdi.model.view.EmployeeTransientVOImpl;
import com.adfdi.model.view.EmployeeTransientVORowImpl;

import com.adfdi.model.view.EmployeesVOImpl;
import com.adfdi.model.view.EmployeesVORowImpl;
import com.adfdi.model.view.QueryParameterVOImpl;

import com.adfdi.model.view.QueryParameterVORowImpl;

import java.util.ArrayList;

import java.util.Calendar;
import java.util.List;

import java.util.Random;

import oracle.jbo.JboException;
import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;
import oracle.jbo.server.ViewRowImpl;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Mon Mar 18 16:15:46 CST 2013
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class AppModuleImpl extends ApplicationModuleImpl implements AppModule {
    /**
     * This is the default constructor (do not remove).
     */
    public AppModuleImpl() {
    }
    // initialize some empty rows as placeholder let user input values
    public void initEmptyRows(){
        EmployeeTransientVOImpl transientVOImpl = 
            this.getEmployeeTransientVO1();
        // Here we just create 20 empty rows
        for(int i =0; i < 5 ; i++){
            ViewRowImpl row = 
                (ViewRowImpl)transientVOImpl.createRow();
            row.setNewRowState(Row.STATUS_INITIALIZED);
            transientVOImpl.insertRow(row);
        }
    }
    

    /**
     * Container's getter for DepartmentsVO1.
     * @return DepartmentsVO1
     */
    public DepartmentsVOImpl getDepartmentsVO1() {
        return (DepartmentsVOImpl)findViewObject("DepartmentsVO1");
    }


    /**
     * Container's getter for QueryParameterVO1.
     * @return QueryParameterVO1
     */
    public QueryParameterVOImpl getQueryParameterVO1() {
        return (QueryParameterVOImpl)findViewObject("QueryParameterVO1");
    }
    
    
    
    

    
    
    public void initRowsFromTemplate(){
        ViewObjectImpl srcVOImpl = this.getEmployeesVO2();
        EmployeeTransientVOImpl transientVOImpl = this.getEmployeeTransientVO1();
        EmployeesVORowImpl.AttributesEnum[] ed=  EmployeesVORowImpl.AttributesEnum.staticValues();
        List<String> attrList  = new ArrayList<String>();
        for(EmployeesVORowImpl.AttributesEnum att:ed){
            attrList.add(att.name());
        }
        srcVOImpl.executeQuery();
        int a = 1;
        while(srcVOImpl.hasNext()){
            ViewRowImpl next = (ViewRowImpl)srcVOImpl.next();    
            ViewRowImpl row = (ViewRowImpl)transientVOImpl.createRow();
            //row.setNewRowState(Row.STATUS_INITIALIZED);
            a++;
            if(a%2==1){
                row.setAttribute("ReadOnly","Y");
            }
            copyValues(next, row,attrList);
            transientVOImpl.insertRow(row);
        }
    }
    
    private void copyValues(Row src, Row target,List<String> attrList){
        for(String attr:attrList){
            Object value = src.getAttribute(attr);
            target.setAttribute(attr, value);
        }
    }
    
    public void addEmployee(){
        ViewObjectImpl srcVOImpl = this.getEmployeesVO2();
        EmployeesVORowImpl.AttributesEnum[] ed=  EmployeesVORowImpl.AttributesEnum.staticValues();
        List<String> attrList  = new ArrayList<String>();
        for(EmployeesVORowImpl.AttributesEnum att:ed){
            attrList.add(att.name());
        }
        EmployeeTransientVOImpl voImpl = this.getEmployeeTransientVO1();
        RowSetIterator iterator = voImpl.createRowSetIterator(null);
        while(iterator.hasNext()){
            Row row = iterator.next();
            ViewRowImpl emp = (ViewRowImpl)srcVOImpl.createRow();
            copyValues(row, emp,attrList);
            voImpl.insertRow(row);
//            String lastName = (String)row.getAttribute("LastName");
//            Integer deptId = (Integer)row.getAttribute("DepartmentId");
//            Integer managerId = (Integer)row.getAttribute("ManagerId");
        }
    }
    
    public void queryEmployeeByDeptId(Integer deptId){
        EmployeesVOImpl srcVOImpl = this.getEmployeesVO2();
        srcVOImpl.setdeptId(deptId);
        srcVOImpl.setApplyViewCriteriaName("EmployeesVOCriteria");
        srcVOImpl.executeQuery();
    }
    
    
    public void queryEmployeeByDeptIdForAuto(){
        Integer deptId = null;
        ViewObjectImpl queryVO = this.getQueryVO1();
        Row row = queryVO.getCurrentRow();
        deptId = (Integer)row.getAttribute("Deptid");
        EmployeesVOImpl srcVOImpl = this.getEmployeesVO1();
        srcVOImpl.setdeptId(deptId);
        srcVOImpl.setApplyViewCriteriaName("EmployeesVOCriteria");
        srcVOImpl.executeQuery();
    }
    
    public void initQueryVOForAuto(){
        ViewObjectImpl queryVO = this.getQueryVO1();
        queryVO.executeEmptyRowSet();
        Row row = queryVO.createRow();
        queryVO.insertRow(row);
    }

    /**
     * Container's getter for CountriesVO1.
     * @return CountriesVO1
     */
    public ViewObjectImpl getCountriesVO1() {
        return (ViewObjectImpl)findViewObject("CountriesVO1");
    }

    /**
     * Container's getter for EmployeesVO2.
     * @return EmployeesVO2
     */
    public EmployeesVOImpl getEmployeesVO2() {
        return (EmployeesVOImpl)findViewObject("EmployeesVO2");
    }

    /**
     * Container's getter for EmployeeTransientVO1.
     * @return EmployeeTransientVO1
     */
    public EmployeeTransientVOImpl getEmployeeTransientVO1() {
        return (EmployeeTransientVOImpl)findViewObject("EmployeeTransientVO1");
    }

    /**
     * Container's getter for QueryVO1.
     * @return QueryVO1
     */
    public ViewObjectImpl getQueryVO1() {
        return (ViewObjectImpl)findViewObject("QueryVO1");
    }
    
    public void saveDepartment(){
        ViewObjectImpl vo = this.getDepartmentsVO1();
        ViewObjectImpl voimpl = this.getDeptTransVO1();
        Row next = voimpl.getCurrentRow();
        Random rn = new Random();
        DepartmentsVORowImpl row = (DepartmentsVORowImpl)vo.createRow();
        
        row.setDepartmentName((String)next.getAttribute("DepartmentName"));
        row.setManagerId((Integer)next.getAttribute("ManagerId"));
        
        String locationName = (String)next.getAttribute("LocationName");
//        String[] ids = locationName.split(":");
        row.setLocationId(1800);
        Integer id = rn.nextInt(300);
        row.setDepartmentId(id);
        vo.insertRow(row);
    }
    
    public void queryDeptByLocationId(String locationId){
        DepartmentsVOImpl vo = this.getDepartmentsVO1();
        if(locationId !=null){
            vo.setloc_Id(Integer.parseInt(locationId));    
        } else {
            vo.setloc_Id(1800);    
        }
        vo.setApplyViewCriteriaName("QueryDetpByLocID");
        vo.executeQuery();
        RowSetIterator itor = vo.createRowSetIterator(null);
        ViewObjectImpl voimpl = this.getDeptTransVO1();
        while(itor.hasNext()){
            Row next = itor.next();
            Row row = voimpl.createRow();
            row.setAttribute("DepartmentId", next.getAttribute("DepartmentId"));
            row.setAttribute("DepartmentName", next.getAttribute("DepartmentName"));
            
            row.setAttribute("LocationId", next.getAttribute("LocationId"));
            row.setAttribute("ManagerId", next.getAttribute("ManagerId"));
            row.setAttribute("LocationName", "Name:"+next.getAttribute("LocationId"));
            row.setNewRowState(Row.STATUS_INITIALIZED);
            voimpl.insertRow(row);
        }
    }
    
    
    public void insertAfterRowAction(){
        EmployeeTransientVOImpl transientVOImpl = this.getEmployeeTransientVO1();
        EmployeeTransientVORowImpl source = (EmployeeTransientVORowImpl)transientVOImpl.getCurrentRow();
        // do validatin here
        valid(source);
        EmployeesVOImpl employeeVOImpl = this.getEmployeesVO2();
        Row target = employeeVOImpl.createRow();
        
        String[] attrs ={"Email","FirstName",
                         "LastName","PhoneNumber","Salary","DepartmentId","JobId","ManagerId"};
        copyValues(source, target, attrs);
        Object pk = System.currentTimeMillis();
        source.getJobId();
        
        target.setAttribute("EmployeeId", pk);
        target.setAttribute("HireDate", Calendar.getInstance().getTime());
        employeeVOImpl.insertRow(target);
    }
    
    private void valid(EmployeeTransientVORowImpl row){
        String lastName = row.getLastName();
        String email = row.getEmail();
        if(lastName == null || email == null || lastName.trim().length() ==0 || email.trim().length()==0){
            throw new JboException("Last Name and email can not be null");
        }
    }
    
    private void copyValues(Row srcRow, Row targetRow, String[] attrs){
        for(String att:attrs){
            Object value = srcRow.getAttribute(att);
            targetRow.setAttribute(att, value);
        }
    }
    
    public void clearupDummyRows(){
        this.getEmployeeTransientVO1().executeEmptyRowSet();
    }
    
    public void intDummyRow(){
          EmployeeTransientVOImpl vo = this.getEmployeeTransientVO1();
          if(vo.getRowCount() > 0) return;
          Row row = vo.createRow();
          vo.insertRow(row);
    }
    
    public void trunOnValidate(){
        this.getSession().getUserData().put("ADFDI_VALIDATION_MODE","False");
    }
    public void trunOffValidate(){
        this.getSession().getUserData().put("ADFDI_VALIDATION_MODE","True");
    }

    /**
     * Container's getter for DeptTransVO1.
     * @return DeptTransVO1
     */
    public ViewObjectImpl getDeptTransVO1() {
        return (ViewObjectImpl)findViewObject("DeptTransVO1");
    }

    /**
     * Container's getter for EmplDIVO1.
     * @return EmplDIVO1
     */
    public ViewObjectImpl getEmplDIVO1() {
        return (ViewObjectImpl)findViewObject("EmplDIVO1");
    }

    /**
     * Container's getter for EmployeesVO1.
     * @return EmployeesVO1
     */
    public EmployeesVOImpl getEmployeesVO1() {
        return (EmployeesVOImpl)findViewObject("EmployeesVO1");
    }
}
